<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : mail_content.manage.php   FILE_PATH : \\system\mail_content.manage.php
 * ....邮件发送内容管理
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue Sep 18 17:22:08 CST 2007
 */
require_once("../global.inc.php");

class SysMailContentManage extends SystemFrameWork{
	/**
	 * 邮件发送内容对象
	 *
	 * @var obj
	 */
	var $obj_mail;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;

	function main(){
		/**
		 * 邮件发送内容对象
		 */
		if (!is_object($this->obj_mail)){
			require_once("mailcontent.class.php");
			$this->obj_mail = new MailContentClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}

		/**
		 * 语言包
		 */
		$this->getlang("sys_mailcontent.manage");

		switch($this->_input['action']){
			case "add":
				$this->_add_mail_content();
				break;
			case "addsave":
				$this->_add_save_mail_montent();
				break;
			case "modi":
				$this->_get_mail_content();
				break;
			case "update":
				$this->_modi_mail_content();
				break;
			case "list":
				$this->_get_mail_contentList();
				break;
			case "set_state":
				$this->_set_state();
				break;
			case "del":
				$this->_del_mail_content();
				break;
			case "open":
				$this->_open();
				break;
			case "close":
				$this->_close();
				break;
			default:
				$this->_get_mail_contentList();
		}
	}
	/**
	 * 列出邮件发送内容列表
	 *
	 */
	function _get_mail_contentList(){
		$this->obj_page->pagebarnum(13);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$mail_array = $this->obj_mail->getMailContentList($this->obj_page);
		if (is_array($mail_array)){
			foreach ($mail_array as $k => $v){
				$mail_array[$k]['content'] = str_replace("\"","'",$v['content']);
			}
		}
		$page_list = $this->obj_page->show(2);
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output("mail_array", $mail_array);
		$this->output("page_list", $page_list);
		$this->showpage("sys_mailcontent.list");
	}
	/**
	 * 显示邮件内容
	 *
	 */
	function _get_mail_content(){
		$mail_array = $this->obj_mail->getMailContent($this->_input['genre']);
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->_input['curpage']);
		$this->output("mail_content_array", $mail_array);   //输出发送邮件内容
		$this->output("radio_mail_open", Common::showForm_Radio("rdoopen","",$this->_b_config['openstate'],$mail_array['ifopen']));
		$this->showpage("sys_mailcontent.modi");
	}
	/**
	 * 修改邮件内容
	 *
	 */
	function _modi_mail_content(){
		$this->obj_mail->modiMailContent($this->_input);
		/*记录操作日志*/
		SystemPowerClass::addSysLog($this->_lang['lanagMailSysteSetContentSetAmendTemplate']);
		$this->redirectPath('error','./mailcontent.manage.php?action=list&curpage='.$this->_input['curpage'],$this->_lang['lanagMailAmendOk'],1);
	}

	/**
	 * 新增邮件内容
	 */
	function _add_mail_content(){
		/**
		 * 页面输出
		 */
		$this->showpage("sys_mailcontent.add");
	}

	/**
	 * 保存新增邮件内容
	 */
	function _add_save_mail_montent(){
		/**
		 * 语言包
		 */
		$this->getlang("system");
		/**
		 * 验证表单信息
		 */
		$this->obj_validate->validateparam = array(
		array("input"=>$this->_input["txtMailTitle"],"require"=>"true","message"=>$this->_lang['errMailTitle']),
		array("input"=>$this->_input["txtMailGenre"],"require"=>"true","message"=>$this->_lang['errMailGenre']),
		array("input"=>$this->_input["txtMaildesc"],"require"=>"true","message"=>$this->_lang['errMaildesc']),
		array("input"=>$this->_input["txtContent"],"require"=>"true","message"=>$this->_lang['errMailContent']),
		);
		$error = $this->obj_validate->validate();
		if($error != ""){
			$this->redirectPath('error','',$error,1);
		}else {
			/**
			 * 判断邮件标识是否重复
			 */
			$array = $this->obj_mail->getMailContent($this->_input['txtMailGenre']);
			if (!empty($array)){
				$url = "./system/mailcontent.manage.php?action=add";
				$this->redirectPath("succ",$url,$this->_lang['errMailGenreRepeat']);
			}
			if ($this->obj_mail->addMailContent($this->_input)) {
				/*记录操作日志*/
				SystemPowerClass::addSysLog($this->_lang['LangMailSysSetContentSetAdd']);
				$this->redirectPath('error','./mailcontent.manage.php?action=list',$this->_lang['lanagMailTemplateAddOk'],1);
			}else {
				$this->redirectPath('error','',$this->_lang['langCOperatorLost'],1);
			}
		}
	}

	/**
	 * 设置邮件发送状态
	 */
	function _set_state(){
		$state = $this->_input['state'];//改变状态
		if ($this->obj_mail->setState($this->_input['id'],$state)) {
			if($state == '1'){
				//记录操作日志
				SystemPowerClass::addSysLog($this->_lang['lanagMailLogSetOpen'].$this->_input['id']);
			}else if($state == '0'){
				//记录操作日志
				SystemPowerClass::addSysLog($this->_lang['lanagMailLogSetClose'].$this->_input['id']);
			}
			$this->redirectPath('error','./mailcontent.manage.php?action=list',$this->_lang['lanagMailSetOk'],1);
		}else {
			$this->redirectPath('error','',$this->_lang['langCOperatorLost'],1);
		}
	}

	/**
	 * 删除非系统的邮件模板
	 */
	function _del_mail_content(){
		if (!empty($this->_input['mail_id'])) {
			//判断是否存在可删除的邮件模板
			if (empty($this->_input['can_del_arr'])) {
				$this->redirectPath('error','',$this->_lang['lanagMailNotDelSystemMail'],1);
			}
			$i = 0;
			foreach ($this->_input['mail_id'] as $v){
				if (in_array($v,$this->_input['can_del_arr'])) {
					$result = $this->obj_mail->delMailContent($v);
					if ($result === false) {
						$this->redirectPath('error','./mailcontent.manage.php?action=list&curpage='.intval($this->_input['curpage']),$this->_lang['langCOperatorLost'],1);
					}
					$i++;
				}
			}
			if ($i > 0) {
				//记录操作日志
				SystemPowerClass::addSysLog($this->_lang['lanagMailLogDel']);
				$this->redirectPath('error','./mailcontent.manage.php?action=list&curpage='.intval($this->_input['curpage']),$this->_lang['lanagMailDeleteOk'],1);
			}else {
				$this->redirectPath('error','',$this->_lang['lanagMailNotDelSystemMail'],1);
			}
		}elseif ($this->_input['id'] != ''){
			$result = $this->obj_mail->delMailContent($this->_input['id']);
			if ($result === false) {
				$this->redirectPath('error','./mailcontent.manage.php?action=list&curpage='.intval($this->_input['curpage']),$this->_lang['langCOperatorLost'],1);
			}
			//记录操作日志
			SystemPowerClass::addSysLog($this->_lang['lanagMailLogDel']);
			$this->redirectPath('error','./mailcontent.manage.php?action=list&curpage='.intval($this->_input['curpage']),$this->_lang['lanagMailDeleteOk'],1);
		}else {
			$this->redirectPath('error','',$this->_lang['lanagMailSelIsDelEmpty'],1);
		}
	}

	/**
	 * 设置邮件发送状态
	 */
	function _open(){
		if(!empty($this->_input['mail_id'])){
			foreach($this->_input['mail_id'] as $v){
				$this->obj_mail->setState($v,'1');
			}
			//记录操作日志
			SystemPowerClass::addSysLog($this->_lang['lanagMailLogSetOpen'].implode(',',$this->_input['mail_id']));
			$this->redirectPath('error','',$this->_lang['lanagMailSetOk'],1);
		}else{
			$this->redirectPath('error','',$this->_lang['lanagMailSelIsEmpty'],1);
		}
	}

	/**
	 * 设置邮件发送状态
	 */
	function _close(){
		if(!empty($this->_input['mail_id'])){
			foreach($this->_input['mail_id'] as $v){
				$this->obj_mail->setState($v,'0');
			}
			//记录操作日志
			SystemPowerClass::addSysLog($this->_lang['lanagMailLogSetClose'].implode(',',$this->_input['mail_id']));
			$this->redirectPath('error','',$this->_lang['lanagMailSetOk'],1);
		}else{
			$this->redirectPath('error','',$this->_lang['lanagMailSelIsEmpty'],1);
		}
	}
}

$mail_content = new SysMailContentManage();
$mail_content->main();
unset($mail_content);
?>